<?php


namespace app\common\controller;
use app\common\model\Agent;
use app\common\model\Merchant;
use app\common\model\Paylog;

class WnObjApi
{

    public function wn_order_query($payinfo)
    {
        try {
            $Paylog = new Paylog();
            $Merchant = new Merchant();
            $Agent = new Agent();
            $map1['id'] = $payinfo['mid'];
            $config = $Merchant->where($map1)->find();
            $agent = $Agent->where(array('id' => $config['agent']))->find();
            //END
            $api_param['appid'] = $agent['sign_appid'];
            $api_param['method'] = "orderquery";
            $api_param['merchantid'] = $payinfo['mid'];
            $api_param['out_trade_no'] = $payinfo['orderid'];
            $api_param['source'] = "online";
            $api_param['version'] = "2.0";
            $api_param['timestamp'] = time();
            $api_param['signaturenonce'] = rand(1000000, 9999999);
            $ApiSign = new ApiSign();
            $signparam = $ApiSign->uni_array($api_param);
            $api_param['sign'] = $ApiSign->getsign($signparam, "MD5");
            $url = "https://api.wunengkeji.com/api.php";
            $res_param = $this->http_post($url, json_encode($api_param, JSON_UNESCAPED_UNICODE));
            $res_param = json_decode($res_param, true);
        } catch (\Exception $e) {
            $res_param['return_code'] = "FAIL";
            $res_param['return_msg'] = $e->getMessage();;
//            file_put_contents('/data2/html/yuanqu/property/log/wn_order_query_error.log', json_encode($res_param,JSON_UNESCAPED_UNICODE), 8);
        }

        return $res_param;
    }


    public function http_get($url)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    public function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        } elseif ($post_file) {

            if ($is_curlFile) {
                foreach ($param as $key => $val) {

                    if (substr($val, 0, 1) == '@') {

                        $param[$key] = new \CURLFile(realpath(substr($val, 1)));

                    }
                }

            }
            $strPOST = $param;

        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);

        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type:application/json; charset=utf-8',

            'Content-Length: ' . strlen($param)));
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

}